package com.lanyam.travel.base.aspect;

import com.alibaba.fastjson.JSONObject;
import com.lanyam.travel.base.annotation.Permission;
import com.lanyam.travel.base.entity.base.LoginSubject;
import com.lanyam.travel.base.entity.base.Payload;
import com.lanyam.travel.base.exception.ErrorParametersException;
import com.lanyam.travel.base.exception.UnloginException;
import com.lanyam.travel.base.utils.JwtUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author KingTang
 * @date   2019/12/20
 */
@Aspect
@Component
@Order(1)
public class PermissionAspect extends BaseElinkcareController{

//
//    @Pointcut("@annotation(com.lanyam.travel.base.annotation.Permission)")
//    public void permissionControlCut(){
//        //do nothing
//    }
//
//    @Around(value = "permissionControlCut()")
//    public Object doAroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
//
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Permission annotation = signature.getMethod().getAnnotation(Permission.class);
//
//        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
//
//        String authorization = request.getHeader(HttpHeaders.AUTHORIZATION);
//        if (authorization == null){
//            throw new ErrorParametersException();
//        }
//        String[] split = authorization.split(" ");
//        if (split.length != 2 ||!"Bearer".equals(split[0])){
//            throw new UnloginException();
//        }
//        String token = split[1];
//        if (token == null){
//            throw new UnloginException();
//        }
//
//        JwtUtils.verify(token);
//        Payload payload = JwtUtils.getPayload(token, Payload.class);
//        Integer deptId = payload.getDeptId();
//        Integer userId = payload.getUserId();
//
//        LoginSubject subject = new LoginSubject();
//        AccountRoleDeptVo accountRoleView = accountMapper.getAccountRoleDeptVo(userId);
//        List<Integer> roleIdList = new ArrayList<>();
//        for (Role role: accountRoleView.getRoleList()){
//            roleIdList.add(role.getRoleId());
//        }
//
//        subject.setUserId(userId);
//        subject.setRealname(accountRoleView.getAccountExt().getRealname());
//        subject.setDeptId(deptId);
//        subject.setDeptName(accountRoleView.getDepartment().getDeptName());
//        subject.setRoleIdList(roleIdList);
//
//        String handle = annotation.handle();
//        if (handle != null && ! handle.equals("")){
//            List<RolehandledeptDepartmentRelation> relationList = rolehandledeptDepartmentRelationMapper.getRolehandledeptDepartmentRelation(userId, handle);
//            if (relationList == null || relationList.size() == 0){
//                throw new PermissionDeniedException();
//            }
//
//            HashMap<String, List<Integer>> map = new HashMap<>();
//            List<Integer> deptIdList = new ArrayList<>();
//            for (RolehandledeptDepartmentRelation relation: relationList){
//
//                String handleDeptIds = relation.getHandleDeptIds();
//                if (handleDeptIds != null){
//                    deptIdList.addAll(JSONObject.parseArray(handleDeptIds, Integer.class));
//                }
//            }
//            subject.setDeptIdList(deptIdList);
//            subject.setMap(map);
//        }
//
//        Object[] args = joinPoint.getArgs();
//        for (int i = 0; i < args.length; i++){
//            Object arg = args[i];
//            if (arg instanceof LoginSubject){
//                args[i] = subject;
//            }
//        }
//
//        return joinPoint.proceed(args);
//    }

}
